package com.test.rxjavademo;

import org.jetbrains.annotations.NotNull;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;

class FitterOperatorDemo {


    private static Observer observer = new Observer<Object>() {//Observer观察者，订阅subscribe了被观察者Observable

        @Override
        public void onSubscribe(@NotNull Disposable disposable) {
            System.out.println("1、onSubscribe,首次订阅时会回调:" + disposable);
        }

        @Override
        public void onNext(@NotNull Object o) {
            System.out.println("onNext:" + o);
        }

        @Override
        public void onError(@NotNull Throwable throwable) {
            System.out.println("onError:" + throwable);
        }

        @Override
        public void onComplete() {
            System.out.println("onComplete");
        }
    };


    public static void filterTest(){
        System.out.println("createTest");

        //Observable被观察者
        Observable.range(1,10)
                .filter(new Predicate<Integer>() {
                    @Override
                    public boolean test(@NotNull Integer integer) throws Exception {

                        return integer%2==0;//满足条件的留下
                    }
                })
                .subscribe(new Observer<Object>() {//Observer观察者，订阅subscribe了被观察者Observable
            @Override
            public void onSubscribe(@NotNull Disposable disposable) {
                System.out.println("1、onSubscribe,首次订阅时会回调:" + disposable);
            }

            @Override
            public void onNext(@NotNull Object o) {
                System.out.println("onNext:" + o);
            }

            @Override
            public void onError(@NotNull Throwable throwable) {
                System.out.println("onError:" + throwable);
            }

            @Override
            public void onComplete() {
                System.out.println("onComplete");
            }
        });
    }





    public static void main(String[] args) {
        filterTest();
    }

}
